package com.i8office.android.zhihuixinwu.ui.dep;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.tools.annotation.ActivityFragmentInject;
import com.cyan.tools.base.BaseActivity;
import com.cyan.tools.base.BaseFragment;
import com.cyan.tools.util.RxBus;
import com.example.menhoo.toollibrary.common.assist.IMEHelper;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.widget.MsgView;
import com.gyf.barlibrary.ImmersionBar;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.bean.TabItem;
import com.i8office.android.zhihuixinwu.ui.OfficeDBFragment;
import com.i8office.android.zhihuixinwu.ui.OfficeFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


@ActivityFragmentInject(
        contentViewId = R.layout.activity_office_new
)
public class OfficeNewActivity extends BaseActivity {
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    String titles[] = {"区级", "部门"};
    private List<BaseFragment> fragments = new ArrayList<>();
//    private Observable<Integer> observable;
//    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f) //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .init();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        fragments.add(OfficeDBFragment.newInstance());
        fragments.add(OfficeBMFragment.newInstance());

        initViewPager();
//        observable = RxBus.get().register("OnGetMsgNum", Integer.class);
//        disposable = observable.subscribe(new Consumer<Integer>() {
//            @Override
//            public void accept(final Integer num) throws Exception {
//                MsgView msgView = tabLayout.getMsgView(2);
////                tabLayout.showMsg(2, 20);
////                tabLayout.setMsgMargin(2,-5,5);
//                msgView.setTextSize(10);
//                if (num > 0) {
//                    tabLayout.showMsg(1, num);
//                } else {
//                    tabLayout.hideMsg(1);
//                }
//            }
//        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (disposable != null && !disposable.isDisposed()) disposable.dispose();
//        if (observable != null) RxBus.get().unregister("OnGetMsgNum", observable);
    }

    //获取Tab
    private void initViewPager() {
        tabLayout.setVisibility(View.VISIBLE);
        ArrayList<TabItem> tabItems = new ArrayList<TabItem>();
        tabItems.add(new TabItem("1"));
        tabItems.add(new TabItem("2"));
        tabItems.add(new TabItem("3"));
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

            @Override
            public int getCount() {
                return titles.length;
            }
        });
        tabLayout.setupWithViewPager(viewpager);
        viewpager.setCurrentItem(getIntent().getIntExtra("page", 0));
    }
}
